import { addMemberAdd, apiUpdateMemberAdd } from '../../api/index'
import { getRoute } from '../../common/js/mixin'

let page = {
  /**
   * 页面的初始数据
   */
  data: {
    consigner: '',
    mobile: '',
    address: '',
    zip_code: '',
    alias: '',
    isEdit: false,
    addIds: {
      province: 1,
      city: 1,
      district: 1
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    if (options.data) {
      let data = JSON.parse(options.data)
      this.setData({
        addIds: {
          province: data.province,
          city: data.city,
          district: data.district
        },
        id: data.id,
        address_info: data.address_info,
        consigner: data.consigner,
        mobile: data.mobile,
        address: data.address,
        zip_code: data.zip_code,
        isEdit: true
      })
    }
    this.setData({
      isIphoneX: getApp().globalData.isIphoneX
    })
  },
  textBlur (e) {
    let index = e.currentTarget.dataset.index
    let value = e.detail.value
    switch (index) {
      case '1':
        this.data.consigner = value
        break
      case '2':
        this.data.mobile = value
        break
      case '3':
        this.data.region = value
        break
      case '4':
        this.data.address = value
        break
      case '5':
        this.data.zip_code = value
    }
  },
  showTaostInfo (t) {
    wx.showToast({
      title: t,
      icon: 'none'
    })
  },
  selectCity (e) {
    this.data.addIds = e.detail
  },
  // 新增地址
  addAddress () {
    //  新增地址
    addMemberAdd({
      ...this.data.addIds,
      token: wx.getStorageSync('token'),
      consigner: this.data.consigner,
      mobile: this.data.mobile,
      address: this.data.address,
      zip_code: this.data.zip_code,
      alias: '1'
    }).then(
      res => {
        wx.showToast({
          title: '新增成功',
          success: () => {
            const router = this.getRoute()
            router.getAddList()
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    )
  },
  // 编辑
  editAddress () {
    apiUpdateMemberAdd({
      ...this.data.addIds,
      id: this.data.id,
      token: wx.getStorageSync('token'),
      consigner: this.data.consigner,
      mobile: this.data.mobile,
      address: this.data.address,
      zip_code: this.data.zip_code,
      alias: '1'
    }).then(
      res => {
        wx.showToast({
          title: '编辑成功',
          success: () => {
            const router = this.getRoute()
            router.getAddList()
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    )
  },
  addNewAddress () {
    if (this.data.consigner) {
      if (/^[1][3,4,5,7,8][0-9]{9}$/.test(this.data.mobile)) {
        if (this.data.address) {
          if (this.data.isEdit) {
            // 编辑
            this.editAddress()
          } else {
            // 新增
            this.addAddress()
          }
        } else {
          this.showTaostInfo('请填写详细地址')
        }
      } else {
        this.showTaostInfo('请填写正确的电话号码')
      }
    } else {
      this.showTaostInfo('请填写收货人姓名')
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {}

}
getRoute(page)
Page(page)
